<?php

/**
 * Controller_affair
 */

class Controller_affair extends MyApp {


    /** 首页 **/
    public function actionIndex()
    {

         $this->tpl->display();
    }
	
	/** 事务交代列表 **/
	public function actionAffairList ()
	{	
		// 部门列表
		$department_list= $this->db->fetchAll("SELECT `department_name`,`department_id` FROM `tbl_department` ");
		// 分页
		YOU::loadLib('page', "", '0');

		$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_student`");
		$count = $tmp['count'];
		$page_total = ceil($count/PAGE_LIMIT);

		// 当前页
		$p = intval($_GET['p']);
		if ($p == 0){
			$p  = 1;
		}
		if ($p > 1){
			$start = PAGE_LIMIT * ($p-1);
		}else{
			$start = 0;
		}

		$affair_lists = $this->db->fetchAll("SELECT * FROM `tbl_affair` ORDER BY affair_id DESC LIMIT {$start}, ".PAGE_LIMIT."");
			
		// 搜索
		if($_POST){
			$affair_title			= $this->post->affair_title;
			$department_id			= $this->post->department_id;
			$affair_transfer		= $this->post->affair_transfer;
			// 序列化转发部门
			$affair_transfer		= serialize($affair_transfer);

			// 标题模糊搜索
		
			if(!empty($affair_title)){
				
				$affair_lists = $this->db->fetchAll("SELECT * FROM `tbl_affair` WHERE `affair_title` LIKE '%{$affair_title}%' ORDER BY affair_id DESC LIMIT {$start}, ".PAGE_LIMIT."");
			}
			// 部门搜索
			if(!empty($department_id)){
				
				$affair_lists = $this->db->fetchAll("SELECT * FROM `tbl_affair` WHERE department_id ={$department_id} ORDER BY affair_id DESC LIMIT {$start}, ".PAGE_LIMIT."");

			}
			// 转发
			if(!empty($affair_transfer)){
				$info = array(   
								
				"affair_transfer"			=> $affair_transfer,
				
				);
				$this->db->do_update("tbl_affair", $info, "affair_id='{$affair_id}'");
				// 跳转
				Helper_Client::location("添加成功", "index.php?a=affair.affairlist");
				}
		}

		foreach($affair_lists as $key=>$value){
			//	部门
			$department_data= $this->db->fetchRow("SELECT `department_name`,`department_id` FROM `tbl_department` WHERE `department_id`={$value['department_id']}");
			$affair_lists[$key]['department_name']=$department_data['department_name'];
		}

		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
		$page->next_page   = '下一页>';
		$page->pre_page    = '<上一页';
		$page->style_path  = "static/page_style";

		$this->tpl->assign("page", $page->show(4, "yahoo"));
		$this->tpl->assign("affair_lists", $affair_lists);
		$this->tpl->assign("department_list", $department_list);
		$this->tpl->display('affair/Affair.list.tpl.php');

	}
	/** 事务添加 **/
	public function actionAffairAdd()
    {   
		// 发布部门
		$department_lists = $this->db->fetchAll("SELECT * FROM `tbl_department`");
		// 通知样式
		$style_lists	  = $this->db->fetchAll("SELECT * FROM `tbl_inform_style`");
	
        if ($_POST){ 
            
			$department_id			= $this->post->department_id;
			$style_id				= $this->post->style_id;
			$affair_time     	    = $this->post->affair_time;
			$affair_title     		= $this->post->affair_title;
			$affair_content     	= $this->post->affair_content;
			$affair_jurisdiction    = $this->post->affair_jurisdiction;
			// 序列化部门数组	
			$affair_jurisdiction	= serialize($affair_jurisdiction);
			
			// 标题格式
			if($style_id =='请选择标题格式'){
				Helper_Client::alert("请选择标题格式");
			}

            // 写入数据库
            $info = array(   
                			
			"department_id"			=> $department_id,
			"style_id"				=> $style_id,
			"affair_time"      		=> $affair_time, 
			"affair_title"			=> $affair_title,
			"affair_content"		=> $affair_content,
			"affair_jurisdiction"	=> $affair_jurisdiction,
            "status"                => 1,
            "create_time"           => date("Y-m-d H:i:s"),
            "update_time"           => "0000-00-00 00:00:00",  
			
            );
			$this->db->do_insert("tbl_affair", $info);
            // 跳转
            Helper_Client::location("添加成功", "index.php?a=affair.affairlist");
        }
       
      	$this->tpl->assign("department_lists", $department_lists);
		$this->tpl->assign("style_lists", $style_lists);
        $this->tpl->display('affair/Affair.add.tpl.php');
    }
	/** 通知回复 **/
    public function actionAffairUpdate()
    {
 
        $inform_id   = (int)$this->get->inform_id;		

        if ($_POST){
            
            $affair_reply	     	= $this->post->affair_reply;
             
            // 写入数据库
            $info = array(  
    
            "affair_reply"			=> $affair_reply, 			 
            "status"                => 1,           
				
            );
     
            $this->db->do_update("tbl_affair", $info, "affair_id='{$affair_id}'");
      
            // 跳转
            Helper_Client::location("回复成功", "index.php?a=affair.affairlist");
        }

        $this->tpl->display('affair/Affair.update.tpl.php');
    }
}
	
?>